package bdcin.servlet.actions.documento;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bdcin.classesbasicas.Documento;
import bdcin.excecoes.BuscarException;
import bdcin.fachada.BDCInFactory;
import bdcin.fachada.IBDCIn;
import bdcin.servlet.actions.Action;

public class RemoverDocumentoAction extends Action {

	@Override
	public void executar(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IBDCIn facade = BDCInFactory.getFacade();
		String id = request.getParameter("id");

		Documento documento=null;
		try {
			documento = facade.buscarDocumento(Long.valueOf(id));

		} catch (BuscarException e) {
			request.setAttribute("mensagem", e.getMessage());
			request.getRequestDispatcher("mensagem.jsp").forward(request, response);
		}
		if(documento != null){
			try {
				facade.removerDocumento(Long.valueOf(id));

			} catch (Exception e) {
				request.setAttribute("mensagem", e.getMessage());
				request.getRequestDispatcher("mensagem.jsp").forward(request, response);
			}	
			request.setAttribute("mensagem", "Documento removido com sucesso.");
			request.getRequestDispatcher("mensagem.jsp").forward(request, response);
		}
		else{
			request.setAttribute("mensagem", "Este documento ja esta excluido!");
			request.getRequestDispatcher("mensagem.jsp").forward(request, response);
		}





		request.getRequestDispatcher("./Controlador?action=documento.BuscarDocumentoRemoverAction&nomeJSP=buscaDocumentoRemover").forward(request,response);


	}
}
